package com.sdyeda.dtu.net;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Server {

	@Value("${com.sdyeda.dtu.dtuport}")
	private int dutport;

	@Value("${com.sdyeda.dtu.iotport}")
	private int iotport;

	@Value("${com.sdyeda.dtu.password}")
	private String password;

	static Map<String, String> set = new HashMap<>();

	private static List<DtuInfo> dtuclients = new ArrayList<DtuInfo>();
	private static List<Socket> iotclients = new ArrayList<Socket>();

	private ServerSocket dtuserver;
	private ServerSocket iotserver;

	private final Log log = LogFactory.getLog(this.getClass());

	@PostConstruct
	public void SaveData() {
		set.put("password", password);
	}

	@PostConstruct
	public void StartDtu() {
		//System.out.println(password);
		Thread dtu = new Thread(() -> {
			try {
				// System.out.println(dutport);
				dtuserver = new ServerSocket(dutport);
				log.info("开启DTU监听服务：" + dutport);
				while (true) {

					Socket dtuclient = dtuserver.accept();
					dtuclient.setOOBInline(true);
					DtuClientThread dutthread = new DtuClientThread(dtuclient);
					dutthread.start();
					dtuclients.add(new DtuInfo(dtuclient));
					
					log.info("DTU"+dtuclient.getInetAddress().getHostAddress()+"接入！");
					//System.out.println(dtuclient.getInputStream().readAllBytes());

				}

			} catch (IOException e) {
				log.error("开启DTU监听服务失败。" + e);
				e.printStackTrace();
			}

		});
		dtu.start();
	}

	@PostConstruct
	public void StartIot() {
		Thread iot = new Thread(() -> {
			try {
				// System.out.println(iotport);
				iotserver = new ServerSocket(iotport);
				log.info("开启IOT监听服务：" + iotport);

				while (true) {

					// System.out.println();
					Socket iotclient = iotserver.accept();
					iotclient.setOOBInline(true);
					IotClientThread iotthread = new IotClientThread(iotclient);
					iotthread.start();
					iotclients.add(iotclient);
					// System.out.println(iotclient);
					log.info("IOT"+iotclient.getInetAddress().getHostAddress()+"接入！");
				}

			} catch (IOException e) {
				log.error("开启IOT监听服务失败。" + e);
				e.printStackTrace();
			}

		});
		iot.start();
	}

	public static List<Socket> getIotclients() {
		iotclients.removeIf(
				item -> (item.isClosed() || !item.isConnected() || item.isInputShutdown() || item.isOutputShutdown()));

		//System.out.println("iots:" + iotclients.size());
		return iotclients;
	}

	public static List<DtuInfo> getDtuclients() {
		dtuclients.removeIf(item -> (item.getSocket().isClosed() || !item.getSocket().isConnected()
				|| item.getSocket().isInputShutdown() || item.getSocket().isOutputShutdown()));

		//System.out.println("iots:" + iotclients.size());
		//System.out.println("dtus:" + dtuclients.size());
		return dtuclients;
	}

	public static String getPassword() {
		return set.get("password");
	}


	//设置DTU编号
	public static void updateDtuId(Socket socket, String id) {
		getDtuclients().forEach(item ->{
			if(item.getSocket().equals(socket)) {
				item.setId(id);
			}
		});
	}

	//设置DTU连接传感器
	public static void updateIot(Socket socket, IotInfo iot) {
		getDtuclients().forEach(item ->{
			if(item.getSocket().equals(socket)) {
				item.updateIot(iot);
			}
		});
	}
}
